<?php

class UserController extends Controller {
	
	public function filters() {
		return array ('accessControl' );
	}
	public function accessRules() {
		return array (array ('deny', 'actions' => array ('image' ), 'expression' => '$user->isGuest' ) );
	
	}
	public function actionRegister() {
		
		$model = new RegisterForm ();
		
		// if it is ajax validation request
		if (isset ( $_POST ['ajax'] ) && $_POST ['ajax'] === 'register-form') {
			echo CActiveForm::validate ( $model );
			Yii::app ()->end ();
		}
		if (isset ( $_POST ['RegisterForm'] )) {
			$model->attributes = $_POST ['RegisterForm'];
      
			// validate user input and redirect to the previous page if valid
			if ($model->validate () && $model->register ()) {
        /*
         * save the init page data
         * */
				@CEmailHelper::wellcome($model->email,$model->email);
				
				$loginForm = new LoginForm ();
				$loginForm->username = $model->email;
				$loginForm->password = $model->password;
				if (! $loginForm->login ()) {
					$this->redirect ( Yii::app ()->user->loginUrl );
				} else
					$this->redirect ( Yii::app ()->user->returnUrl );
			
			}
		}
		$this->render ( 'register', array ('model' => $model ) );
	}
	/**
	 * 1. weibo login.
	 * 2. create user register.
	 * 3. 
	 */
	public function actionWblogin($pid =  Providor::SINA) {
		
		if (isset ( $_GET ['oauth_verifier'] )) {
			$last_key = WeiboApiFactory::getAccessToken ( $pid, $_GET ['oauth_verifier'] );
			//oauth_token    'oauth_token_secret' 
			if (isset ( $last_key ['oauth_token'] )) {
				if (! isset ( $_GET ['pid'] ))
					$this->redirect ( '/site/login' );
				
				$pid = $_GET ['pid'];
				
				//if here is a guest, then register it first.
				

				$up = UserProvidor::model ()->findByAttributes ( array ('puid' => $last_key ['user_id'] ) );
				$user = new User ();
				// print_r ( $up );
				if ($up == null) { //first login.
					//					Yii::app ()->user->setState ( 'lastkey', $last_key );
					$random_passwd = CUtilHelper::random ();
					Yii::app ()->user->setState ( 'random_passwd', $random_passwd );
					
					//register user.
					//					$user->email = $this->email;
					$user->password = md5 ( $random_passwd );
//					$user->name = $user->muid ;
					//					$user->name = $this->email;
					//					$user->domain = $this->domain;
					if ($user->save ()) {
						$up = new UserProvidor ();
						
						$up->pid = $pid;
						$up->uid = $user->_id;
						$up->puid = $last_key ['user_id'];
						$up->type = Providor::$AUTH_TYPE [$pid];
						$up->aKey = $last_key ['oauth_token'];
						$up->sKey = $last_key ['oauth_token_secret'];
						
						if ($up->save ()) {
							//sync info from weibo.
							$weiboBpo = WeiboBPO::instance();
							$weiboBpo->sync($up,$user);
						}
					}else{
						print_r($user->getErrors());
					}
				
				} else {
					Yii::trace ( 'up.uid' . $up->uid );
					$user = User::model ()->findByUid ( $up->uid );
				}
				Yii::app()->user->setState ( 'uid', $user->_id );
				Yii::app()->user->setState ( 'me', (object)$user->attributes );
				Yii::app()->user->login ( new UserIdentity ( time(), null ), 0 );
				
				//store
				if($user->email==null) $this->redirect ( array ('setting/wbRegister')); else $this->redirect ( array ('setting/customizeDomain' ) );
			}
		
		} else {
			$this->redirect(WeiboApiFactory::authorizeURL($pid,'/user/wblogin'));
//			$this->redirect ( array ('setting/account' ) );
		}
	
	}
	public function actionImage($type = 1) {
		$uid = Yii::app ()->user->uid;
		$user = User::model ()->findByPk ( new MongoId ( $uid ) );
		//		if($type == 1)
		$pic = $user->photo;
		//		else 
		//			$pic = $user->pic;
		$this->redirect ( array ("/picture/thumbnail", 'pid' => $pic, 'width' => 50, 'height' => 50 ) );
	}
	
	public function actionPicture($width = 640, $height = 400) { //TODO change
		$uid = Yii::app ()->user->uid;
		$user = Picture::model ()->findByAttributes ( array ('uid' => new MongoId ( $uid ) ) );
		//		if($type == 1)
		$pic = $user->_id;
		//		else 
		//			$pic = $user->pic;
		$this->redirect ( array ("/picture/thumbnail", 'pid' => $pic, 'width' => $width, 'height' => $height ) );
	}
	   
}